<template>
  <div class="my-activities">
    <h2>我参与的活动</h2>
    
    <!-- 添加查询表单 -->
    <el-form :inline="true" class="search-form">
      <el-form-item label="活动标题">
        <el-input v-model="searchForm.title" placeholder="请输入活动标题" />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.status" placeholder="请选择状态">
          <el-option label="全部" value="" />
          <el-option label="进行中" value="进行中" />
          <el-option label="待定" value="待定" />
          <el-option label="已完成" value="已完成" />
        </el-select>
      </el-form-item>
      <el-form-item label="活动时间">
        <el-date-picker
          v-model="searchForm.timeRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="filteredActivities">
      <el-table-column prop="activityId" label="活动编号" width="100" />
      <el-table-column prop="title" label="活动标题" />
      <el-table-column prop="startTime" label="开始时间" width="180" />
      <el-table-column prop="endTime" label="结束时间" width="180" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="score" label="考核分数" width="100" />
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button 
            v-if="canCheckIn(scope.row)"
            type="primary" 
            @click="handleCheckIn(scope.row)"
          >
            签到
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getUserActivities } from '@/api/user'
import { checkInActivity } from '@/api/activity'

export default {
  data() {
    return {
      activities: [],
      searchForm: {
        title: '',
        status: '',
        timeRange: []
      }
    }
  },
  created() {
    this.fetchActivities()
  },
  methods: {
    async fetchActivities() {
      try {
        const userId = this.$store.state.user.id
        const data = await getUserActivities(userId, this.searchForm)
        this.activities = data
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    async handleCheckIn(activity) {
      try {
        await checkInActivity(activity.activityId)
        this.$message.success('签到成功')
        this.fetchActivities()
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  }
}
</script>

<style scoped>
.search-form {
  margin-bottom: 20px;
  background-color: #f5f7fa;
  padding: 20px;
  border-radius: 4px;
}
</style> 